<xsl:stylesheet
    version="1.0"
    exclude-result-prefixes="x xsl cmswrt cbq"
    xmlns:x="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cmswrt="http://schemas.microsoft.com/WebPart/v3/Publishing/runtime"
    xmlns:cbq="urn:schemas-microsoft-com:ContentByQueryWebPart">
  <xsl:output method="html" indent="no" />
  <xsl:param name="cbq_isgrouping" />
  <xsl:param name="cbq_columnwidth" />
  <xsl:param name="Group" />
  <xsl:param name="GroupType" />
  <xsl:param name="cbq_iseditmode" />
  <xsl:param name="cbq_viewemptytext" />
  <xsl:param name="SiteId" />
  <xsl:param name="WebUrl" />
  <xsl:param name="PageId" />
  <xsl:param name="WebPartId" />
  <xsl:param name="FeedPageUrl" />
  <xsl:param name="FeedEnabled" />
  <xsl:param name="SiteUrl" />
  <xsl:param name="BlankTitle" />
  <xsl:param name="BlankGroup" />
  <xsl:param name="UseCopyUtil" />
  <xsl:param name="DataColumnTypes" />
  <xsl:param name="ClientId" />
  <xsl:template match="/">
    <xsl:call-template name="OuterTemplate" />
  </xsl:template>


  <xsl:template name="OuterTemplate">
    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" />
    <xsl:variable name="RowCount" select="count($Rows)" />

    <table id="cbqwp" cellspacing="0" cellpadding="0" class="cbq-layout-main">
      <tr height="20">
        <td colspan="4">
          <div id="am-filters" />
          <script type="text/javascript">
            buildFilters("am-filters");
          </script>
        </td>
      </tr>
      <tr>
        <td colspan="4">
          <table class="am-grid-header" cellspacing="0" cellpadding="0">
            <tr>
              <td style="width:5px">
                <img src="/Style Library/AMAccelerator/Images/mid_round_tab_lh.gif" alt="" />
              </td>
              <td class="am-grid-headerbar" valign="middle">
                <table width="100%">
                  <tr>
                    <td class="am-grid-headerbaritem am-grid-sort" width="65px" onmouseenter="openSortDropdown(this);" onmouseleave="closeSortDropdown(this);">
                      Sort
                      <img src="/Style Library/AMAccelerator/Images/down_arrow.gif" alt="Sort" />
                      <div class="am-grid-sortdropdown">
                        <ul>
                          <li>
                            <a href="javascript:sortGrid('rating');" style="color:white;">Rating</a>
                          </li>
                          <li>
                            <a href="javascript:sortGrid('downloads');" style="color:white;">Downloads</a>
                          </li>
                          <li>
                            <a href="javascript:sortGrid('comments');" style="color:white;">Comments</a>
                          </li>
                          <li>
                            <a href="javascript:sortGrid('title');" style="color:white;">Title</a>
                          </li>
                          <li>
                            <a href="javascript:sortGrid('published');" style="color:white;">Published</a>
                          </li>
                          <li>
                            <a href="javascript:sortGrid('name');" style="color:white;">Name</a>
                          </li>
                          <li>
                            <a href="javascript:sortGrid('size');" style="color:white;">Size</a>
                          </li>
                          <li>
                            <a href="javascript:sortGrid('duration');" style="color:white;">Duration</a>
                          </li>
                        </ul>
                      </div>
                    </td>
                    <td class="am-grid-headerbaritem" width="170px">
                      <a style="color:white;" href="/Pages/Download.aspx">Download Your Selection</a>
                    </td>
                    <td class="am-grid-headerbaritem">

                    </td>
                    <td align="right" class="am-grid-headerbaritem">
                      <xsl:if test="$FeedEnabled = 'True'">
                        <xsl:variable name="FeedUrl1" select="concat($SiteUrl,$FeedPageUrl,'xsl=1&amp;web=',$WebUrl,'&amp;page=',$PageId,'&amp;wp=',$WebPartId)" />
                        <a href="{cmswrt:RegisterFeedUrl( $FeedUrl1, 'application/rss+xml')}">
                          <img src="/Style Library/AMAccelerator/Images/rss.gif" border="0" alt="{cmswrt:GetPublishingResource('CbqRssAlt')}"/>
                        </a>
                      </xsl:if>
                    </td>
                  </tr>
                </table>
              </td>
              <td style="width:5px">
                <img src="/Style Library/AMAccelerator/Images/mid_round_tab_rh.gif" alt="" />
              </td>
            </tr>
          </table>
        </td>
      </tr>
      <xsl:choose>
        <xsl:when test="$RowCount=0">
          <xsl:call-template name="OuterTemplate.Empty">
            <xsl:with-param name="EditMode" select="$cbq_iseditmode" />
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="OuterTemplate.Body">
            <xsl:with-param name="Rows" select="$Rows" />
            <xsl:with-param name="RowCount" select="$RowCount" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
      <tr>
        <td colspan="4">
          <table class="am-grid-header" cellspacing="0" cellpadding="0">
            <tr>
              <td style="width:5px">
                <img src="/Style Library/AMAccelerator/Images/mid_round_tab_lh.gif" alt="" />
              </td>
              <td class="am-grid-headerbar" style="width:100%; text-align:right;">1 2 3 4 Next&gt;</td>
              <td style="width:5px">
                <img src="/Style Library/AMAccelerator/Images/mid_round_tab_rh.gif" alt="" />
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </xsl:template>

  <xsl:template name="OuterTemplate.Empty">
    <xsl:param name="EditMode" />
    <tr>
      <td>
        <xsl:if test="$EditMode = 'True'">
          <div class="wp-content description">
            <xsl:value-of disable-output-escaping="yes" select="$cbq_viewemptytext" />
          </div>
        </xsl:if>
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="OuterTemplate.Body">
    <xsl:param name="Rows" />
    <xsl:param name="RowCount" />

    <xsl:for-each select="$Rows">
      <xsl:variable name="Pos" select="position()" />
      <xsl:choose>
        <xsl:when test="$Pos=1">
          <xsl:value-of select="string('&lt;tr&gt;')" disable-output-escaping="yes" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:if test="$Pos mod 4 = 1">
            <xsl:value-of select="string('&lt;/tr&gt;&lt;tr&gt;')" disable-output-escaping="yes" />
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
      <td class="gal_item_wraper" valign="top" style="margin:0px;">
        <div class="am-griditem">
          <xsl:call-template name="OuterTemplate.CallItemTemplate" />
        </div>
      </td>
    </xsl:for-each>
    <xsl:choose>
      <xsl:when test="$RowCount mod 4 = 1">
        <td class="am-blankitem"></td>
        <td class="am-blankitem"></td>
        <td class="am-blankitem"></td>
      </xsl:when>
      <xsl:when test="$RowCount mod 4 = 2">
        <td class="am-blankitem"></td>
        <td class="am-blankitem"></td>
      </xsl:when>
      <xsl:when test="$RowCount mod 4 = 3">
        <td class="am-blankitem"></td>
      </xsl:when>
    </xsl:choose>

    <xsl:value-of select="string('&lt;/tr&gt;')" disable-output-escaping="yes" />
  </xsl:template>

  <xsl:template name="OuterTemplate.CallItemTemplate">
    <xsl:choose>
      <xsl:when test="@Style='AmGridviewItem'">
        <xsl:apply-templates select="." mode="itemstyle">
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="." mode="itemstyle">
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="OuterTemplate.GetSafeLink">
    <xsl:param name="UrlColumnName"/>
    <xsl:if test="$UseCopyUtil = 'True'">
      <xsl:value-of select="concat('/_layouts/CopyUtil.aspx?Use=id&amp;Action=dispform&amp;ItemId=',@ID,'&amp;ListId=',@ListId,'&amp;WebId=',@WebId,'&amp;SiteId=',$SiteId)"/>
    </xsl:if>
    <xsl:if test="$UseCopyUtil != 'True'">
      <xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
        <xsl:with-param name="UrlColumnName" select="$UrlColumnName"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="OuterTemplate.GetSafeUserLink">
    <xsl:param name="SharePointUser" />
    <xsl:variable name="Id" select="substring-before($SharePointUser, ';')" />
    <xsl:variable name="Name">
      <xsl:call-template name="OuterTemplate.GetUserNameFromSharePointUser">
        <xsl:with-param name="SharePointUser" select="$SharePointUser" />
      </xsl:call-template>
    </xsl:variable>

    <a class="am-userlink" href="/Pages/AuthorDetail.aspx?ID={$Id}">
      <xsl:value-of select="$Name" />
    </a>

  </xsl:template>

  <xsl:template name="OuterTemplate.GetUserNameFromSharePointUser">
    <xsl:param name="SharePointUser" />
    <xsl:choose>
      <xsl:when test="contains($SharePointUser, '\')">
        <xsl:value-of select="substring-after($SharePointUser, '\')" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="substring-after($SharePointUser, '#')" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="OuterTemplate.GetTitle">
    <xsl:param name="Title"/>
    <xsl:param name="UrlColumnName"/>
    <xsl:if test="string-length($Title) != 0">
      <xsl:value-of select="$Title"/>
    </xsl:if>
    <xsl:if test="string-length($Title) = 0">
      <xsl:if test="$UseCopyUtil = 'True'">
        <xsl:value-of select="$BlankTitle" />
      </xsl:if>
      <xsl:if test="$UseCopyUtil != 'True'">
        <xsl:call-template name="OuterTemplate.GetPageNameFromUrl">
          <xsl:with-param name="UrlColumnName" select="$UrlColumnName"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="OuterTemplate.FormatColumnIntoUrl">
    <xsl:param name="UrlColumnName"/>
    <xsl:variable name="Value" select="@*[name()=$UrlColumnName]"/>
    <xsl:if test="contains($DataColumnTypes,concat(';',$UrlColumnName,',URL;'))">
      <xsl:call-template name="OuterTemplate.FormatValueIntoUrl">
        <xsl:with-param name="Value" select="$Value"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="not(contains($DataColumnTypes,concat(';',$UrlColumnName,',URL;')))">
      <xsl:value-of select="$Value"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="OuterTemplate.FormatValueIntoUrl">
    <xsl:param name="Value"/>
    <xsl:if test="not(contains($Value,', '))">
      <xsl:value-of select="$Value"/>
    </xsl:if>
    <xsl:if test="contains($Value,', ')">
      <xsl:call-template name="OuterTemplate.Replace">
        <xsl:with-param name="Value" select="substring-before($Value,', ')"/>
        <xsl:with-param name="Search" select="',,'"/>
        <xsl:with-param name="Replace" select="','"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="OuterTemplate.Replace">
    <xsl:param name="Value"/>
    <xsl:param name="Search"/>
    <xsl:param name="Replace"/>
    <xsl:if test="contains($Value,$Search)">
      <xsl:value-of select="concat(substring-before($Value,$Search),$Replace)"/>
      <xsl:call-template name="OuterTemplate.Replace">
        <xsl:with-param name="Value" select="substring-after($Value,$Search)"/>
        <xsl:with-param name="Search" select="$Search"/>
        <xsl:with-param name="Replace" select="$Replace"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="not(contains($Value,$Search))">
      <xsl:value-of select="$Value"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="OuterTemplate.GetSafeStaticUrl">
    <xsl:param name="UrlColumnName"/>
    <xsl:variable name="Url">
      <xsl:call-template name="OuterTemplate.FormatColumnIntoUrl">
        <xsl:with-param name="UrlColumnName" select="$UrlColumnName"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="cmswrt:EnsureIsAllowedProtocol($Url)"/>
  </xsl:template>

  <xsl:template name="OuterTemplate.GetColumnDataForUnescapedOutput">
    <xsl:param name="Name"/>
    <xsl:param name="MustBeOfType"/>
    <xsl:if test="contains($DataColumnTypes,concat(';',$Name,',',$MustBeOfType,';'))">
      <xsl:value-of select="@*[name()=$Name]"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="OuterTemplate.GetPageNameFromUrl">
    <xsl:param name="UrlColumnName"/>
    <xsl:variable name="Url">
      <xsl:call-template name="OuterTemplate.FormatColumnIntoUrl">
        <xsl:with-param name="UrlColumnName" select="$UrlColumnName"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:call-template name="OuterTemplate.GetPageNameFromUrlRecursive">
      <xsl:with-param name="Url" select="$Url"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="OuterTemplate.GetPageNameFromUrlRecursive">
    <xsl:param name="Url"/>
    <xsl:choose>
      <xsl:when test="contains($Url,'/') and substring($Url,string-length($Url)) != '/'">
        <xsl:call-template name="OuterTemplate.GetPageNameFromUrlRecursive">
          <xsl:with-param name="Url" select="substring-after($Url,'/')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$Url"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="OuterTemplate.GetGroupName">
    <xsl:param name="GroupName"/>
    <xsl:param name="GroupType"/>
    <xsl:choose>
      <xsl:when test="string-length(normalize-space($GroupName)) = 0">
        <xsl:value-of select="$BlankGroup"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$GroupType='URL'">
            <xsl:variable name="Url">
              <xsl:call-template name="OuterTemplate.FormatValueIntoUrl">
                <xsl:with-param name="Value" select="$GroupName"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="OuterTemplate.GetPageNameFromUrlRecursive">
              <xsl:with-param name="Url" select="$Url"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$GroupName" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="OuterTemplate.CallPresenceStatusIconTemplate">
    <xsl:if test="string-length(@SipAddress) != 0">
      <span class="presence-status-icon">
        <img src="/_layouts/images/imnhdr.gif" onload="IMNRC('{@SipAddress}')" ShowOfflinePawn="1" alt="" id="{concat('MWP_pawn_',$ClientId,'_',@ID,'type=sip')}"/>
      </span>
    </xsl:if>
  </xsl:template>

  <xsl:template name="OuterTemplate.FormatDate">
    <xsl:param name="DateTime" />
    <!-- get date info -->
    <xsl:variable name="year">
      <xsl:value-of select="substring($DateTime,1,4)" />
    </xsl:variable>
    <xsl:variable name="month-temp">
      <xsl:value-of select="substring-after($DateTime,'-')" />
    </xsl:variable>
    <xsl:variable name="month">
      <xsl:value-of select="substring-before($month-temp,'-')" />
    </xsl:variable>
    <xsl:variable name="day-temp">
      <xsl:value-of select="substring-after($month-temp,'-')" />
    </xsl:variable>
    <xsl:variable name="day">
      <xsl:value-of select="substring($month-temp,1,2)" />
    </xsl:variable>
    <!-- return -->
    <xsl:value-of select="$month" />
    <xsl:value-of select="'/'" />
    <xsl:value-of select="$day" />
    <xsl:value-of select="'/'" />
    <xsl:value-of select="$year" />
  </xsl:template>

</xsl:stylesheet>
